package com.ems.config;

import com.ems.entity.Admin;
import com.ems.entity.Student;
import com.ems.entity.Teacher;
import com.ems.service.AdminService;
import com.ems.service.StudentService;
import com.ems.service.TeacherService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    @Autowired
    AdminService adminService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权了");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer position = (Integer) session.getAttribute("position");
        if(position == 0){
            Student current = (Student) subject.getPrincipal();
            info.addStringPermission(current.getPermission());
            return info;
        }
        else if(position == 1)
        {
            Teacher current = (Teacher) subject.getPrincipal();
            info.addStringPermission(current.getPermission());
            return info;
        }
        else{
            Admin current = (Admin) subject.getPrincipal();
            info.addStringPermission(current.getPermission());
            return info;
        }
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("认证了");
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MyToken userToken = (MyToken) token;
        session.setAttribute("position",userToken.getPosition());
        if(userToken.getPosition() == 0){
            //学生登录
            Student student = studentService.queryByUsername(userToken.getUsername());
            if(student == null){
                return null;
            }
            return new SimpleAuthenticationInfo(student,student.getPassword(),"");
        }
        else if(userToken.getPosition() == 1){
            //教师登录
            Teacher teacher = teacherService.queryByUsername(userToken.getUsername());
            if(teacher == null){
                return null;
            }
            return new SimpleAuthenticationInfo(teacher,teacher.getPassword(),"");
        }
        else{
            Admin admin = adminService.queryByUsername(userToken.getUsername());
            if(admin == null){
                return null;
            }
            return new SimpleAuthenticationInfo(admin,admin.getPassword(),"");
        }
    }
}
